package com.ndp.fb.walle;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.ndp.fb.config.BaseConfig;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.walle.config.FilterFastJsonRestHttpMessageConverter;
import com.ndp.fb.walle.config.RestExceptionFilter;
import com.ndp.fb.walle.config.json.ComplexPropertyPreFilter;
import com.ndp.fb.walle.filters.TicketValidateClientFilter;
import com.ndp.fb.walle.filters.TokenValidateClientFilter;
import com.thetransactioncompany.cors.CORSFilter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.IOException;

/**
 * Created by hunk.qin on 2015/1/26.
 */
public class ApplicationWalle extends BaseConfig {

    @Override
    public ApplicationConfig registerApplicationConfig() {
        return new ApplicationConfig(ConfigConst.WALLE_JAVA_ID);
    }
    @Override
    public String getRegistryGroup() {
        return ConfigConst.DUBBO_REGISTER_GROUP;
    }


    @Bean
    public FilterRegistrationBean corsFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CORSFilter corsFilter = new CORSFilter();
        registrationBean.setFilter(corsFilter);
        registrationBean.setOrder(1);
        return registrationBean;
    }


    @Bean
    public ComplexPropertyPreFilter complexPropertyPreFilter() throws IOException, ClassNotFoundException {
        ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter();
        return filter;
    }

    @Bean
    public HttpMessageConverters customConverters() throws IOException, ClassNotFoundException {
        FilterFastJsonRestHttpMessageConverter fastJsonHttpMessageConverter = new FilterFastJsonRestHttpMessageConverter();
        fastJsonHttpMessageConverter.setSerializeFilter(complexPropertyPreFilter());
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }


    @Bean
    public TokenValidateClientFilter tokenValidateClientFilter() {
        return new TokenValidateClientFilter();
    }

    @Bean
    TicketValidateClientFilter userFilter() {
        return new TicketValidateClientFilter();
    }

    @Bean
     public RestExceptionFilter filter(){
        return new RestExceptionFilter();
    }


    @Bean
    public ThreadPoolTaskExecutor walleThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(20);
        return taskExecutor;
    }
}
